package com.sh.序列化;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeExample {
    public static void main(String[] args) throws Exception {
        Person p = new Person("Alice", 25);

        // 2️⃣ 序列化到文件
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.txt"));
        out.writeObject(p);
        out.close();

        // 3️⃣ 反序列化回来
        ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.txt"));
        Person p2 = (Person) in.readObject();
        in.close();

        System.out.println("反序列化结果：" + p2);
    }
}